package com.iflytek.autoCompareTool.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author llchen12
 * @date 2018/5/31
 */
public class FileUtil {

    private static Logger logger = LoggerFactory.getLogger(FileUtil.class);


    public static boolean fileToZip(String sourceFilePath, String targetFilePath, final String fileName) {

        File sourceFile = new File(sourceFilePath);
        if (!sourceFile.exists()) {
            logger.warn("待压缩的文件目录：{} 不存在.", sourceFilePath);
            return false;
        }

        boolean res;

        File zipFile;
        String zipFileName = fileName;
        int i = 1;
        while (true) {
            zipFile = new File(targetFilePath + "/" + zipFileName + ".zip");
            if (zipFile.exists()) {
                logger.warn("{} 目录下存在名字为: {}.zip 的打包文件", targetFilePath, zipFileName);
                zipFileName = fileName + "(" + i++ + ")";
            } else {
                break;
            }
        }


        try (FileOutputStream fos = new FileOutputStream(zipFile);
             BufferedOutputStream bos = new BufferedOutputStream(fos);
             ZipOutputStream zos = new ZipOutputStream(bos)) {

            File[] files = null;
            if (sourceFile.isDirectory()) {
                files = sourceFile.listFiles();
            }

            if (files == null || files.length == 0) {
                files = new File[]{sourceFile};
            }

            byte[] bytes = new byte[1024];
            FileInputStream fis;
            BufferedInputStream bis;
            ZipEntry zipEntry;
            for (File file : files) {
                //创建ZIP实体，并添加进压缩包
                zipEntry = new ZipEntry(file.getName());
                zos.putNextEntry(zipEntry);
                //读取待压缩的文件并写进压缩包里
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int read;
                while ((read = bis.read(bytes)) != -1) {
                    zos.write(bytes, 0, read);
                }
                fis.close();
                bis.close();
            }
            res = true;

        } catch (IOException e) {
            logger.error("读写错误", e);
            res = false;
        }
        return res;

    }


    public static boolean delete(String fileOrPath) {
        File file = new File(fileOrPath);
        if (!file.exists()) {
            return true;
        }
        return delete(file);
    }


    public static boolean delete(String... fileOrPath) {
        if (fileOrPath == null || fileOrPath.length == 0) {
            return true;
        }
        for (String f : fileOrPath) {
            if (!delete(f)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 删除文件或文件夹
     */
    public static boolean delete(File fileOrDirectory) {
        if (!fileOrDirectory.exists()) {
            return true;
        }

        if (fileOrDirectory.isFile()) {
            return fileOrDirectory.delete();
        }

        File[] files = fileOrDirectory.listFiles();
        if (files == null || files.length == 0) {
            return fileOrDirectory.delete();
        }

        for (File f : files) {
            if (f.isFile()) {
                if (!f.delete()) {
                    return false;
                }
            } else {
                delete(f);
            }
        }

        return fileOrDirectory.delete();
    }

    public static void main(String[] args) {
        System.out.println(delete("D:\\test\\temp\\1528421378498"));
    }


}
